/*
 * Copyright 2008 FBK (http://www.fbk.eu/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fbk.it.hlt.jlsi.data;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import cern.colt.matrix.impl.AbstractMatrix2D;
import cern.colt.matrix.impl.DenseDoubleMatrix2D;

/**
 * Reads in Sparse Binary Matrix File.
 * 
 * @author Claudio Giuliano
 * @version %I%, %G%
 * @since 1.0
 */
public class DenseBinaryMatrixFileReader implements MatrixFileReader {
	/**
	 * Define a static logger variable so that it references the Logger instance
	 * named <code>DenseBinaryMatrixFileReader</code>.
	 */
	static Logger logger = Logger.getLogger(DenseBinaryMatrixFileReader.class
			.getName());

	//
	private RandomAccessFile raf;

	/**
	 * The number of rows.
	 */
	private int nr;

	/**
	 * The number of columns.
	 */
	private int nc;

	/**
	 * Constructs a sparse binary matrix writer.
	 * 
	 * @param file
	 *            the file where to write the matrix.
	 */
	public DenseBinaryMatrixFileReader(File f) throws IOException {
		raf = new RandomAccessFile(f, "rw");

		nr = 0;
		nc = 0;

		readHeader();

	} // end constructor

	/**
	 * Reads the number of rows, columns and non zero values. All values are
	 * 4-byte integers.
	 */
	private void readHeader() throws IOException {
		raf.seek(0);

		// read the number of rows
		nr = raf.readInt();

		// read the number of columns
		nc = raf.readInt();

		logger.debug("matrix: " + nr + " " + nc);
	} // end readHeader

	/**
	 * Reads the number of non zero values for a column and or each non-zero
	 * value in the column: row index and value. All values are 4-byte integers
	 * except value, which is a 4-byte float. All are in network byte order.
	 * 
	 * @param indexes
	 *            the column indexes.
	 * @param values
	 *            the column values.
	 */
	public AbstractMatrix2D read() throws IOException {

		// float[][] matrix = new float[nc][nr];
		DenseDoubleMatrix2D matrix = new DenseDoubleMatrix2D(nc, nr);

		for (int i = 0; i < nr; i++) {
			System.out.print(i + " ");
			for (int j = 0; j < nc; j++) {
				// read the value
				// matrix[j][i] = raf.readFloat();
				matrix.setQuick(j, i, (double) raf.readFloat());

				// logger.debug(i + ", " + j + " = " + matrix[j][i]);
			} // end for j
		} // end for i

		return matrix;
	} // end read

	//
	public void close() throws IOException {
		raf.close();
	} // end close

	//
	public static void main(String[] args) throws Exception {
		String logConfig = System.getProperty("log-config");
		if (logConfig == null)
			logConfig = "log-config.txt";

		long begin = System.currentTimeMillis();

		PropertyConfigurator.configure(logConfig);

		if (args.length != 1) {
			System.out
					.println("Usage: java -mx1024M org.fbk.it.hlt.jlsi.data.DenseBinaryMatrixFileReader file");
			System.exit(1);
		}

		File file = new File(args[0]);

		DenseBinaryMatrixFileReader matrixFileReader = new DenseBinaryMatrixFileReader(
				file);
		// float[][] matrix = matrixFileReader.read();
		DenseDoubleMatrix2D matrix = (DenseDoubleMatrix2D) matrixFileReader
				.read();

		logger.debug("matrix: " + matrix.rows() + " X " + matrix.columns());
		for (int i = 0; i < matrix.rows(); i++) {
			for (int j = 0; i < matrix.columns(); j++) {

				System.out.print(matrix.getQuick(i, j) + " ");
			} // end for j
			System.out.print("\n");
		} // end for i
		long end = System.currentTimeMillis();
		System.out.println("matrix wrote in " + (end - begin) + " ms");
	} // end main

} // end DenseBinaryMatrixFileReader